import java.util.Scanner;
import java.util.ArrayList;

public class SnakeRect{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        SnakeDir[] dirArr = new SnakeDir[4];    //snake step direction
        dirArr[0] = new SnakeDir(1, 0);         //down
        dirArr[1] = new SnakeDir(0, -1);        //left
        dirArr[2] = new SnakeDir(-1, 0);        //up
        dirArr[3] = new SnakeDir(0, 1);         //right
        
        System.out.print("请输入蛇形矩阵的宽度(即高度): ");
        int n = scan.nextInt();
        
        int[][] snakeArr = new int[n][n];  
        //先给前n项赋值     
        for(int i = 0; i < n; i++){             
            snakeArr[0][i] = i + 1;
        }
         
        boolean snakeTired = false;     //蛇是否疲劳
        int snakeStep = n - 1;          //蛇每次在一个方向上的步数，初始值n-1
        int snakeRow = 0;               //蛇初始纵坐标
        int snakeCol = n - 1;           //蛇初始横坐标
        
        int dir = 0;                    //蛇初始方向0，即down，向下
        int stepBackUp = snakeStep;     //蛇步数备份
        for(int i = n; i < n*n; i++){
            if(snakeStep <= 0){         //判断当前方向上是否步数走完
                ++dir;                  //走完就换方向
                if(!snakeTired){        //然后判断蛇是否疲劳，不疲劳步数不变
                    snakeTired = true;
                    snakeStep = stepBackUp;
                }else{                  //疲劳则步数-1
                    snakeTired = false;
                    snakeStep = stepBackUp = stepBackUp - 1;
                }
            }
            snakeRow += dirArr[dir % 4].row;
            snakeCol += dirArr[dir % 4].col;
            snakeArr[snakeRow][snakeCol] = i + 1;
            --snakeStep;
        }//for
        
        for(int i = 0; i < snakeArr.length; i++){
            for(int j = 0; j < snakeArr[i].length; j++){
                System.out.print(snakeArr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

class SnakeDir{
    public final int row;
    public final int col;
    public SnakeDir(int row, int col){
        this.row = row;
        this.col = col;
    }
}